package pe.org.manuela.app.despachadores

import groovy.xml.*
import pe.org.manuela.app.*
import pe.org.manuela.app.beans.*

static def obtenerInsumos() {
	def url = Main.URL
	def data = new URL("$url/MostrarInsumos.groovy").text
	data = new XmlParser().parseText(data)
	data = data.collect {
		[
			id: it.id.text() as int,
			descripcion: it.descripcion.text(),
			estado: it.estado.text()
		] as Insumo
	}
	return data
}

static def ingresarInsumo(params) {
	def url = Main.URL
	params = params.collect{
		key, value ->
		if (!("$value".isInteger())) value = URLEncoder.encode(value)
		"$key=$value"
	}.join("&")
	def data = new URL("$url/IngresarInsumo.groovy?$params").text
	data = new XmlParser().parseText(data)
	data = data.collect{ 
		[
			success: it
		]
	}
	return data
}

static def modificarInsumo(params) {
	def url = Main.URL
	params = params.collect{
		key, value ->
		if (!("$value".isInteger())) value = URLEncoder.encode(value)
		"$key=$value"
	}.join("&")

	def data = new URL("$url/ModificarInsumo.groovy?$params").text
	data = new XmlParser().parseText(data)
	data = data.collect{ 
		[
			success: it
		]
	}
	return data
}

static def obtenerInsumosPorTalla(params) {
	def url = Main.URL
	params = params.collect{
		key, value ->
		if (value && !("$value".isInteger())) value = URLEncoder.encode(value)
		"$key=$value"
	}.join("&")	
	def data = new URL("$url/MostrarInsumosPorTalla.groovy?$params").text
	data = new XmlParser().parseText(data)
	data = data.collect {
		[
			id: it.id.text() as int,
			descripcion: it.descripcion.text()
		] as Insumo
	}
	System.out.println(data)
	return data	
}
